/* eslint-disable */
import axios from "axios";
import _ from "lodash";
import qs from "qs";
import { ElMessage } from "element-plus";

const generateApiMap = (map) => {
    let facade = {};
    _.forEach(map, function(value, key) {
        facade[key] = toMethod(value);
    });
    return facade;
};

//整合配置
const toMethod = (options) => {
    options.method = options.method || "post";
    return (params, config = {}) => {
        return sendApiInstance(options.method, options.url, params, config);
    };
};

// 创建axios实例
const createApiInstance = (config = {}) => {
    const _config = {
        withCredentials: false, // 跨域是否
        baseURL: "",
        validateStatus: function(status) {
            if (status != 200) {
                ElMessage.error(status + "：后台服务异常");
            }
            return status;
        }
    };
    config = _.merge(_config, config);
    return axios.create(config);
};

//入参前后去空格
const trim = (param) => {
    for (let a in param) {
        if (typeof param[a] == "string") {
            param[a] = param[a].trim();
        } else {
            param = trim(param[a]);
        }
    }
    return param;
};

//restful路径参数替换
const toRest = (url, params) => {
    let paramArr = url.match(/(?<=\{).*?(?=\})/gi);
    paramArr.forEach((el) => {
        url = url.replace("{" + el + "}", params[el]);
    });
    return url;
};

//封装请求体
const sendApiInstance = (method, url, params, config = {}) => {
    params = trim(params);
    if (!url) {
        return;
    }
    let instance = createApiInstance(config);
    // instance.interceptors.request.use((res) => {
    //     console.log(res);
    //     return res;
    // });
    //响应拦截
    instance.interceptors.response.use(
        (response) => {
            let data = response; //服务端返回数据
            let code = data.code; //返回信息状态码
            let message = data.message; //返回信息描述
            // if (data === undefined || typeof data != "object") {
            //     ElMessage.error("后台对应服务异常");
            //     return false;
            // } else {
            return data.data;
            // }
            // else if (code != 0) {
            //     ElMessage.error(message);
            //     return false;
            // }
        },
        (error) => {
            return Promise.reject(error).catch((res) => {
                console.log(res);
            });
        }
    );
    //请求方式判断
    let _method = "";
    let _params = {};
    let _url = "";
    if (method === "form") {
        _method = "post";
        config.headers = { "Content-Type": "application/x-www-form-urlencoded" };
        _params = qs.stringify(params);
        _url = url;
    } else if (method === "resetful") {
        _method = "get";
        _params = {};
        _url = toRest(url, params);
    } else if (method === "get") {
        _method = "get";
        _params = {
            params: params
        };
        _url = url;
    } else if (method === "post") {
        _method = "post";
        _params = params;
        _url = url;
    } else if (method === "put") {
        config.headers = { "Content-Type": "application/json;charSet=UTF-8" };
        _method = "put";
        _params = params;
        _url = url;
    } else {
        ElMessage.error("请求方式不存在");
    }
    return instance[_method](_url, _params, config);
};

export default {
    generateApiMap: generateApiMap
};